/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.chl.da.controllerBeans;

import edu.chl.da.ejb.ctrl.IManageDestination;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;

/**
 * Controller bean for add destination events.
 * @author Erik
 */
@ManagedBean(name = "AddDestinationBean")
@RequestScoped
public class AddDestinationBean {

    private String name, image, errorMsg;
    private String geoX, geoY;
    private boolean success;

    @EJB
    IManageDestination imd;

    /** Creates a new instance of AddDestinationBean */
    public AddDestinationBean() {
        this.errorMsg = "";
    }
    
    public void addDestination(ActionEvent e)
            throws AbortProcessingException {
        if(name == null ||
           geoX == null ||
           geoY == null ||
           image == null ||
           ("").equals(name) ||
           ("").equals(geoX.toString()) ||
           ("").equals(geoY.toString()) ||
           ("").equals(image)){
            this.success = false;
            this.errorMsg = "Alla falt maste vara ifyllda.";
        }
        else{
            this.success = true;
            imd.createDestination(name, image, Double.parseDouble(geoX), Double.parseDouble(geoY));
        }
    }

    public String navigate(){
        if(success){
            
            return "/admin/manageDestinations.jsf";
        }
        else{
            FacesContext.getCurrentInstance().addMessage(null,
            new FacesMessage(errorMsg));
            return null; //Same page;
        }
    }

    public String getGeoX() {
        return geoX;
    }

    public void setGeoX(String geoX) {
        this.geoX = geoX;
    }

    public String getGeoY() {
        return geoY;
    }

    public void setGeoY(String geoY) {
        this.geoY = geoY;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
